// pages/write-note/write-note.js
import util from "../../../utils/util"
Page({

    /**
     * 页面的初始数据
     */
    data: {
      gzhList: [],
      nomoreshow: false,
      nomore: false,
      onloadType: 0,  //0表示正常加载数据,1表示搜索数据
      search: "",
      skip: 0
    },
    clear_placeholder()
    {
        this.setData({
          noteContext2:""
        })
    },
    toGzhPost(e) {
      wx.navigateTo({
          url: '../gzhWebView/gzhWebView?path=' + util.baseEncode(e.currentTarget.dataset.link),
      })
  },
    uploadPic(){
        wx.chooseImage({
          count: 1,
        }).then(res=>{
            console.log(res.tempFilePaths[0])
            wx.showLoading({
              title: '上传中',
            })
            wx.$cloud.uploadFile({
                cloudPath: new Date().getTime()+"noteImg.png",
                filePath: res.tempFilePaths[0]
            }).then(res=>{
                console.log(res.fileID)
                let temp_pic = this.data.notePicture;
                temp_pic.push(res.fileID);
                this.setData({
                    notePicture: temp_pic
                })
                wx.hideLoading({
                  success: (res) => {},
                })
            })
        }) 
    },
    JumpToSearch(){
      const that = this
      that.setData({
        onloadType: 1,
        skip: 0,
        gzhList: [],
        nomore: false,
        nomoreshow: false
      })
      that.getData()
    },
    bindKeyInput(e) {
      this.setData({
          search: e.detail.value
      })
  },
    async deleteImg(e){
        wx.showLoading({
          title: '删除中',
          mask:true
        })
        console.log(e.currentTarget.dataset.index)
        let index = e.currentTarget.dataset.index;
        let temp_pic = this.data.notePicture;
        await wx.$cloud.deleteFile({
            fileList: [temp_pic[index]]
          })
        temp_pic.splice(index,1);
        this.setData({
            notePicture: temp_pic
        })
        wx.hideLoading({
          success: (res) => {},
        })
    },
    writeNote(e){
        console.log(e.detail.value)
        this.setData({
            noteContext:e.detail.value
        })
    },
    save() {
        console.log(this.data.openid)
        wx.showLoading({
          title: '保存中',
        })
        wx.$cloud.callFunction({
            name: "userModule",
            data: {
                action: "addNote",
                openid: this.data.openid,
                category:this.data.category,
                question: this.data.questionContent,
                selection: this.data.selection,
                title:this.data.examName,
                noteContext:this.data.noteContext,
                notePicture:this.data.notePicture
            }
        }).then(res=>{
            wx.hideLoading({
              success: (res) => {},
            })
            wx.showToast({
              title: '保存成功',
            })
            setTimeout(() => {
                wx.navigateBack({
                    delta: 0,
                  })
            }, 1000);
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      console.log('页面加载')
      wx.showLoading({
        title: '请稍候...',
        duration:1000
      })
        const that = this
        that.setData({
          province: options.province,
          city: options.city,
          gzhList: [],
          skip: 0,
          nomore: false,
          nomoreshow: false,
          onloadType: 0
        })
        that.getData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      const that = this
      that.setData({
        skip: that.data.skip+10
      })
      console.log("************skip:",that.data.skip)
      that.getData()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    goBack() {
        wx.navigateBack({})
    },
    getData(){
      const that = this
      if(that.data.onloadType == 0){
            wx.$cloud.callFunction({
              name: 'getGZH',
              data: {
                  Action: 'getList',
                  skip: that.data.skip,
                  size: 10
              },
              success(r) {
                console.log("res:",r)
                  var res = r.result
                  if(!res.no_more){
                    that.setData({
                      gzhList: that.data.gzhList.concat(res.data),
                      nomore: false
                    })
                  }else{
                    if(that.data.gzhList.length == 0){
                      that.setData({
                        nomoreshow: true,
                        nomore: false
                      })
                    }else {
                    that.setData({
                      nomore: true
                    })
                  }
                  }
              }
          })
    }
    else if(that.data.onloadType == 1){
      console.log("搜素----------------")
      wx.$cloud.callFunction({
        name: 'getGZH',
        data: {
            Action: 'getSearch',
            skip: that.data.skip,
            size: 10,
            search: that.data.search
        },
        success(r) {
          console.log("searching-------------")
          console.log("res:",r)
            var res = r.result
            if(!res.no_more){
              that.setData({
                gzhList: that.data.gzhList.concat(res.data),
                nomore: false
              })
            }else{
              if(that.data.gzhList.length == 0){
                that.setData({
                  nomoreshow: true,
                  nomore: false
                })
              }else {
              that.setData({
                nomore: true
              })
            }
            }
        }
    })
    }
  }
})